<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button @click="addVisible = true" type="primary" size="mini"
          >添加分类</el-button
        >
      </div>
      <el-table :data="cateList" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              size="mini"
              @click="editOpenHandler(scope.row)"
              type="primary"
              >修改</el-button
            >
            <el-button
              @click="delHandler(scope.row.id)"
              size="mini"
              type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- :before-close="handleClose" 关闭前的回调 -->
    <el-dialog
      @closed="$refs.addForm.resetFields()"
      title="添加文章分类"
      :visible.sync="addVisible"
      width="35%"
    >
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addHandler"
          >添 加</el-button
        >
      </span>
    </el-dialog>

    <!-- 修改对话框 -->
    <el-dialog
      @closed="$refs.editForm.resetFields()"
      title="修改文章分类"
      :visible.sync="editVisible"
      width="35%"
    >
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="editForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editHandler"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  created () {
    this.getArtCate()
  },
  data () {
    return {
      cateList: [],
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请填写分类名称', tirgger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符',
            tirgger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请填写分类别名', tirgger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            tirgger: 'blur'
          }
        ]
      },
      editVisible: false,
      editForm: {
        cate_name: '',
        cate_alias: ''
      }
    }
  },
  methods: {
    async getArtCate () {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    addHandler () {
      this.$refs.addForm.validate(async (result) => {
        if (!result) return
        const { data: res } = await this.$http.post(
          '/my/cate/add',
          this.addForm
        )
        if (res.code === 0) {
          this.addVisible = false
          this.getArtCate()
        }
      })
    },
    editHandler () {
      this.$refs.editForm.validate(async (result) => {
        if (!result) return
        const { data: res } = await this.$http.put(
          '/my/cate/info',
          this.editForm
        )
        if (res.code === 0) {
          this.editVisible = false
          this.getArtCate()
        }
      })
    },
    editOpenHandler (row) {
      if (row.id === 1 || row.id === 2) {
        return this.$message.error('管理员不允许修改这个分类哦')
      }
      this.editVisible = true
      this.editForm = { ...row }
    },
    async delHandler (id) {
      if (id === 1 || id === 2) {
        return this.$message.error('管理员不允许删除这个哦')
      }
      const result = await this.$confirm('确定删除吗？', '提示').catch((e) => e)
      if (result === 'confirm') {
        const { data: res } = await this.$http.delete('/my/cate/del', {
          params: { id }
        })
        if (res.code === 0) this.getArtCate()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
